//package io.github.talelin.latticy.mq.consumer;
//
//import com.rabbitmq.client.Channel;
//import io.github.talelin.latticy.common.configuration.RabbitConfig;
//import io.github.talelin.latticy.common.constant.Constant;
//import io.github.talelin.latticy.common.pojo.Mail;
//import io.github.talelin.latticy.common.util.MailUtil;
//import io.github.talelin.latticy.model.MsgLogDO;
//import io.github.talelin.latticy.mq.MessageHelper;
//import io.github.talelin.latticy.service.MsgLogService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.core.MessageProperties;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//
//@Component
//@Slf4j
//public class SimpleMailConsumer {
//
//    @Autowired
//    private MsgLogService msgLogService;
//
//    @Autowired
//    private MailUtil mailUtil;
//
//    @RabbitListener(queues = RabbitConfig.MAIL_QUEUE_NAME)
//    public void consume(Message message, Channel channel) throws IOException {
//        Mail mail = MessageHelper.msgToObj(message, Mail.class);
//        log.info("收到消息: {}", mail.toString());
//
//        Long msgId = mail.getMsgId();
//        MsgLogDO msgLog = msgLogService.getById(msgId);
//        if (null == msgLog || msgLog.getStatus().equals(Constant.MsgLogStatus.CONSUMED_SUCCESS)) {// 消费幂等性
//            log.info("重复消费, msgId: {}", msgId);
//            return;
//        }
//
//        MessageProperties properties = message.getMessageProperties();
//        long tag = properties.getDeliveryTag();
//
//        boolean success = mailUtil.send(mail);
//        if (success) {
//            msgLog.setStatus(Constant.MsgLogStatus.CONSUMED_SUCCESS);
//            // 验证定时任务的消息重投 下面这行代码注释
//            msgLogService.updateById(msgLog);
//            channel.basicAck(tag, false);// 消费确认
//        } else {
//            channel.basicNack(tag, false, true);
//        }
//    }
//
//}
